package cn.edu.neusoft.service.event;

import cn.edu.neusoft.config.OauthConfigUtil;
import cn.edu.neusoft.config.PlatformConfig;
import cn.edu.neusoft.model.event.eventSubcribe.SubscribeRequest;
import com.dahuatech.hutool.http.Method;
import com.dahuatech.hutool.json.JSONUtil;
import com.dahuatech.icc.exception.ClientException;
import com.dahuatech.icc.oauth.model.v202010.GeneralResponse;
import com.dahuatech.icc.oauth.model.v202010.OauthConfigUserPwdInfo;
import com.dahuatech.icc.oauth.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SubscribeHandler {
    @Autowired
    private OauthConfigUtil oauthConfigUtil;
    @Autowired
    private PlatformConfig platformConfig;
    public void subscribeAlarmEvent(){
        SubscribeRequest subscribeRequest = new SubscribeRequest();
        SubscribeRequest.Param param = new SubscribeRequest.Param();

        SubscribeRequest.Param.Subsystem subsystem = new SubscribeRequest.Param.Subsystem();
        subsystem.setName(platformConfig.getListenerName());
        subsystem.setMagic(platformConfig.getListenerName());
        param.setSubsystem(subsystem);

        List<SubscribeRequest.Param.Monitor> monitors = new ArrayList();
        SubscribeRequest.Param.Monitor monitor = new SubscribeRequest.Param.Monitor();
        //回调地址的接口代码可参考SubscribeCallBackController类中的receiveMsg方法
        monitor.setMonitor(platformConfig.getListenerUrl());

        List<SubscribeRequest.Param.Monitor.Event>events = new ArrayList<>();
        SubscribeRequest.Param.Monitor.Event event = new SubscribeRequest.Param.Monitor.Event();
        //订阅报警事件，填alarm
        event.setCategory("alarm");
        List<SubscribeRequest.Param.Monitor.Event.Authority> authorities = new ArrayList();
        SubscribeRequest.Param.Monitor.Event.Authority authority=new SubscribeRequest.Param.Monitor.Event.Authority();

        List<String> types= new ArrayList<>();
        types.add("1000001");
        types.add("300101");
        authority.setTypes(types);

        authorities.add(authority);
        event.setAuthorities(authorities);
        events.add(event);
        monitor.setEvents(events);
        monitors.add(monitor);
        param.setMonitors(monitors);
        subscribeRequest.setParam(param);
        log.info("SubscribeDemo,subscribeAlarmEvent,request:{}", JSONUtil.toJsonStr(subscribeRequest));

        OauthConfigUserPwdInfo config = oauthConfigUtil.getOauthConfig();
        try {
            GeneralResponse response = HttpUtils.executeJson("/evo-apigw/evo-event/1.0.0/subscribe/mqinfo", subscribeRequest,null, Method.POST, config,GeneralResponse.class);
            log.info("SubscribeDemo,subscribeAlarmEvent,response:{}", response.getResult());
        } catch (ClientException e) {
            log.error(e.getErrMsg(), e);
        }
    }

    /**
     * 订阅业务事件
     */
    public void subscribeBusinessEvent(){
        SubscribeRequest subscribeRequest = new SubscribeRequest();
        SubscribeRequest.Param param = new SubscribeRequest.Param();

        SubscribeRequest.Param.Subsystem subsystem = new SubscribeRequest.Param.Subsystem();
        subsystem.setName(platformConfig.getListenerName());
        subsystem.setMagic(platformConfig.getListenerName());
        param.setSubsystem(subsystem);

        List<SubscribeRequest.Param.Monitor> monitors = new ArrayList();
        SubscribeRequest.Param.Monitor monitor = new SubscribeRequest.Param.Monitor();

        monitor.setMonitor(platformConfig.getListenerUrl());

        List<SubscribeRequest.Param.Monitor.Event>events = new ArrayList<>();
        SubscribeRequest.Param.Monitor.Event event = new SubscribeRequest.Param.Monitor.Event();

        event.setCategory("business");
        List<SubscribeRequest.Param.Monitor.Event.Authority> authorities = new ArrayList();
        SubscribeRequest.Param.Monitor.Event.Authority authority=new SubscribeRequest.Param.Monitor.Event.Authority();

        List<String> types= new ArrayList<>();
        types.add("car.capture");
        types.add("car.access");
        types.add("vehicle.records");

        authority.setTypes(types);

        authorities.add(authority);
        event.setAuthorities(authorities);
        events.add(event);
        monitor.setEvents(events);
        monitors.add(monitor);
        param.setMonitors(monitors);
        subscribeRequest.setParam(param);
        log.info("SubscribeDemo,subscribeBusinessEvent,request:{}", JSONUtil.toJsonStr(subscribeRequest));

        OauthConfigUserPwdInfo config = oauthConfigUtil.getOauthConfig();
        try {
            GeneralResponse response = HttpUtils.executeJson("/evo-apigw/evo-event/1.0.0/subscribe/mqinfo", subscribeRequest,null, Method.POST, config,GeneralResponse.class);
            log.info("SubscribeDemo,subscribeBusinessEvent,response:{}", response.getResult());
        } catch (ClientException e) {
            log.error(e.getErrMsg(), e);
        }
    }

    public void cancelSubscribe(String name){
        String url = "/evo-apigw/evo-event/1.0.0/subscribe/mqinfo?name="+name;
        log.info("SubscribeDemo,cancelSubscribe,url:{}", JSONUtil.toJsonStr(url));

        OauthConfigUserPwdInfo config = oauthConfigUtil.getOauthConfig();
        try {
            GeneralResponse response = HttpUtils.executeJson(url, null,null, Method.DELETE, config, GeneralResponse.class);
            log.info("SubscribeDemo,cancelSubscribe,response:{}", response);
        } catch (ClientException e) {
            log.error(e.getErrMsg(), e);
        }
    }
}
